package com.fatdetection.demo.controller;

import com.fatdetection.demo.annotation.AuthAdmin;
import com.fatdetection.demo.annotation.AuthOperator;
import com.fatdetection.demo.annotation.AuthUser;
import com.fatdetection.demo.bean.Record;
import com.fatdetection.demo.service.RecordService;
import com.fatdetection.demo.util.QiniuUtils;
import com.fatdetection.demo.util.RstMsgUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RequestMapping("/record")
@RestController
@AuthAdmin
@AuthOperator
public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    QiniuUtils qiniuUtils;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true),
            @ApiImplicitParam(name = "partName", value = "部位名称", required = true),
    })
    @GetMapping("/getRecord/{phone}/{partName}")
    public Object getRecord(@PathVariable String phone, @PathVariable String partName){
        return recordService.getRecord(phone, partName);
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startT", value = "精确到day,格式 y-m-d", required = true),
            @ApiImplicitParam(name = "endT", value = "精确到day,格式 y-m-d", required = true),
            @ApiImplicitParam(name = "userId", value = "user.phone", required = true)
    })
    @GetMapping("/findByCtime")
    @AuthUser
    public Object findByCtime(Date startT, Date endT, String partName, String userId){
        List<Record> byCtime = recordService.findByCtime(startT, endT, partName, userId);
        return RstMsgUtils.success(null, byCtime);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名，请携带文件类型并且全局唯一，例如：record_sichuan_GYg7fg7ggu_20210708.jpg", required = false)
    })
    @PostMapping("/uploadImg")
    public boolean uploadImg(MultipartFile file, String fileName){
        Image img = null;
        try {
            img = ImageIO.read(file.getInputStream());
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        } finally {
            img = null;
        }
        // 这里对文件进行检验
        boolean status = false;
        try {
            if("".equals(fileName)){
                fileName = file.getOriginalFilename();
            }
            status = qiniuUtils.upload(file.getInputStream(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return status;
    }

    @PostMapping("/add")
    public Boolean insertRecord(Record record){
        return recordService.insertRecord(record);
    }

    @PostMapping("/del")
    public Boolean delRecord(Integer id){
        return recordService.delRecord(id);
    }

    @PostMapping("/update")
    public Boolean updateRecord(Record record){
        return recordService.updateRecord(record);
    }

    @GetMapping("/findAll")
    public List<Record> findAll(){
        List<Record> list = recordService.findAll();
        return list;
    }

    @GetMapping("/findByStuId/{userId}")
    @AuthUser
    public List<Record> findByStuId(@PathVariable("userId") String userId){
        List<Record> list = recordService.findByStuId(userId);
        return list;
    }

    @GetMapping("/findByPart/{partName}")
    @AuthUser
    public List<Record> findByPart(@PathVariable("partName") String partName){
        List<Record> list = recordService.findByPart(partName);
        return list;
    }

}
